// pages/wo/shouyiD/shouyiD.js
import request from '../../../utils/http.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    IsLogin: false,
    IsTixian: true, //是否允许提现
    tiXianRiQiTiXing: '',

    dianPuId: 0,
    // 佣金


    zongShouRu: 0, //总输入
    daiTiQu: 0, //
    // yeWuJiang:0,
    // tuiJianJiang:0,
    yuanGongJiang: 0,
    daiFenPei: 0,
    monthRuZhang: 0,
    monthTiQu: 0,

    tabList: ["佣金明细", "提现记录"],
    tabIndex: 0,

    ruZhangList: [],
    tiQuList: [],
    monthRuZhang: 0, //本月入账
    monthTiQu: 0, //本月出账



    date: '',
    mouth: '',
    isDate: false,
    windowHeight: '',
    isgetData: false, //false没有本地数据；true 有数据
    left:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.changeline()
    this.setData({
      IsLogin: wx.getStorageSync(app.globalData.LOGINSTATUS),
      dianPuId: wx.getStorageSync(app.globalData.SHOPID),
    })

    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight - (550 / 750 * res.windowWidth),
        })
      },

    })
    //console.log(wx.getStorageSync(app.globalData.screenHeight)-300)

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

    var date = new Date()
    var zero = '0'
    var mouthStr = date.getMonth() + 1

    if (mouthStr < 10) {
      mouthStr = zero.concat(mouthStr)
    }
    this.setData({
      mouth: date.getFullYear() + '-' + mouthStr
    })
    this.getShouYeMingXi()



  },

  //根据条件，查询收益
  getShouYeMingXi: function () {

    var that = this
    wx.showLoading({
      title: '加载中...',
    })

    let data = {
      dateStr: that.data.date == '' ? that.data.mouth : that.data.date,
      dianPuId:that.data.dianPuId,
    }

    request.get('yongJin/personShouYe', data, res => {
      // console.log("收益:" + JSON.stringify(res.data))
      that.setData({
        zongShouRu: res.data.zongShouRu,
        daiTiQu: res.data.daiTiQu,
        daiFenPei: res.data.daiFenPei,
        yuanGongJiang: res.data.yuanGongJiang,

        ruZhangList: res.data.ruZhangList,
        tiQuList: res.data.tiQuList,
        monthRuZhang: res.data.monthRuZhang,
        monthTiQu: res.data.monthTiQu,
        IsTixian: res.data.shiFouKeYiTiXian == 1 ? true : false,
        tiXianRiQiTiXing: res.data.tiXianRiQiTiXing
      })

      wx.hideLoading()

    }, err => {
      wx.showToast({
        title: "获取收益信息失败！", // 提示的内容
        icon: "none", // 图标，默认success
        duration: 3000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })

  },


  //提现
  bindDetailTap: function () {
    if (this.data.IsTixian) {
      wx.navigateTo({
        url: '/pkg_wo/pages/cashOut/cashOut?leiXing=4&daiTiQuShouYi=' + this.data.daiTiQu,
      })
    } else {
      wx.showToast({
        title: this.data.tiXianRiQiTiXing, // 提示的内容
        icon: "none", // 图标，默认success
        duration: 3000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    }

  },

  //切换tab
  bindTabTap: function (e) {
    var index = e.currentTarget.dataset.index;
    // console.log('index:' + index);
    this.setData({
      tabIndex: index
    })
    if (this.data.tabIndex == 0) {
      //console.log("入账数据:");
      this.setData({
        isgetData: this.data.ruZhangList.length > 0 ? true : false
      })

    } else if (this.data.tabIndex == 1) {
      //console.log("出账益:");
      this.setData({
        isgetData: this.data.tiQuList.length > 0 ? true : false
      })
    }

    this.changeline()
  },


  changeline: function () {
    const query = wx.createSelectorQuery()
    var _this = this
    query.select('.tabTrue').boundingClientRect()
    query.exec(function (res) {

      var width = res[0].width
      var left = res[0].left

      var minddle = res[0].left + width / 2 - 14


      _this.setData({
        left: minddle
      })

    })
  },

  //日选择器
  bindDateChange(e) {
    this.setData({
      date: e.detail.value,
      isDate: true,
    })
    this.getShouYeMingXi()
    this.changeline()
  },


  //月选择器
  bindMouthChange(e) {
    //console.log(e.detail.value)
    this.setData({
      mouth: e.detail.value,
      isDate: false,
    })
    this.getShouYeMingXi()
  },

  //清空日期
  clearnDate: function () {
    this.setData({
      date: '',
      isDate: false,
      tabIndex: 0
    })

    if (this.data.tabIndex == 0) {
      // console.log("入账数据:");
      this.setData({
        isgetData: this.data.ruZhangList.length > 0 ? true : false
      })

    } else if (this.data.tabIndex == 1) {
      //console.log("出账益:");
      this.setData({
        isgetData: this.data.chuZhangList.length > 0 ? true : false
      })
    } else if (this.data.tabIndex == 2) {
      // console.log("后扣益:");
      this.setData({
        isgetData: this.data.houKouList.length > 0 ? true : false
      })

    }

    var date = new Date()
    var zero = '0'
    var mouthStr = date.getMonth() + 1

    if (mouthStr < 10) {
      mouthStr = zero.concat(mouthStr)
    }
    this.setData({
      mouth: date.getFullYear() + '-' + mouthStr
    })

    this.getShouYeMingXi()
    this.changeline()
  }
})